home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 4.iso / src / demos / REALITY / fastshadows / sgiobjgl.c < prev    next >
C/C++ Source or Header  |  1994-08-01  |  13KB  |  423 lines

  1. /*
  2.  * Copyright 1993, 1994, Silicon Graphics, Inc.
  3.  * All Rights Reserved.
  4.  *
  5.  * This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
  6.  * the contents of this file may not be disclosed to third parties, copied or
  7.  * duplicated in any form, in whole or in part, without the prior written
  8.  * permission of Silicon Graphics, Inc.
  9.  *
  10.  * RESTRICTED RIGHTS LEGEND:
  11.  * Use, duplication or disclosure by the Government is subject to restrictions
  12.  * as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
  13.  * and Computer Software clause at DFARS 252.227-7013, and/or in similar or
  14.  * successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
  15.  * rights reserved under the Copyright Laws of the United States.
  16.  */
  17. /*
  18.  *    sgiobjgl -
  19.  *        A simple object format - graphics support.
  20.  *
  21.  *            Paul Haeberli - 1990
  22.  */
  23. #include "stdio.h"
  24. #include "gl.h"
  25. #include "sgiobj.h"
  26. #include "spin.h"
  27.  
  28. sgiobj *newquadobj(nquads)
  29. int nquads;
  30. {
  31.     sgiobj *obj;
  32.  
  33.     obj = (sgiobj *)malloc(sizeof(sgiobj));
  34.     obj->next = 0;
  35.     obj->objtype = OBJ_QUADLIST;
  36.     obj->nlongs = PNTLONGS*4*nquads;
  37.     obj->data = (long *)malloc(obj->nlongs*sizeof(long));
  38.     obj->xnlongs = 0;
  39.     obj->xdata = 0;
  40.     return obj;
  41. }
  42.  
  43. sgiobj *newtriobj(ntri)
  44. int ntri;
  45. {
  46.     sgiobj *obj;
  47.  
  48.     obj = (sgiobj *)malloc(sizeof(sgiobj));
  49.     obj->next = 0;
  50.     obj->objtype = OBJ_TRILIST;
  51.     obj->nlongs = PNTLONGS*3*ntri;
  52.     obj->data = (long *)malloc(obj->nlongs*sizeof(long));
  53.     obj->xnlongs = 0;
  54.     obj->xdata = 0;
  55.     return obj;
  56. }
  57.  
  58. sgiobj *newtmeshobj(nlongs)
  59. int nlongs;
  60. {
  61.     sgiobj *obj;
  62.  
  63.     obj = (sgiobj *)malloc(sizeof(sgiobj));
  64.     obj->next = 0;
  65.     obj->objtype = OBJ_TRIMESH;
  66.     obj->nlongs = nlongs;
  67.     obj->data = (long *)malloc(obj->nlongs*sizeof(long));
  68.     obj->xnlongs = 0;
  69.     obj->xdata = 0;
  70.     return obj;
  71. }
  72.  
  73. sgiobj *readsgiobj(name)
  74. char *name;
  75. {
  76.     FILE *inf;
  77.     sgiobj *obj, *head, *tail;
  78.     int npoints, colors;
  79.     long objtype, plongs, nlongs;
  80.     long magic;
  81.     int i, ntri, nquads;
  82.  
  83.     inf = fopen(name,"r");
  84.     if(!inf) {
  85.         fprintf(stderr,"readsgiobj: can't open input file %s\n",name);
  86.         exit(1);
  87.     }
  88.     fread(&magic,sizeof(long),1,inf);
  89.     if(magic == FASTMAGIC) {
  90.     fread(&npoints,sizeof(long),1,inf);
  91.     fread(&colors,sizeof(long),1,inf);
  92.     nquads = npoints/4; 
  93.     if(colors) {
  94.         fprintf(stderr,"readsgiobj: can't read nonormal spin objects\n");
  95.         exit(1);
  96.     } else
  97.         obj = newquadobj(nquads);
  98.     bzero(obj->data,obj->nlongs*sizeof(long));
  99.     for(i=0; i<npoints; i++) {
  100.         fread(obj->data+(PNTLONGS*i)+OFFSET_NORMAL,3*sizeof(long),1,inf);
  101.         fread(obj->data+(PNTLONGS*i)+OFFSET_POINT,3*sizeof(long),1,inf);
  102.         bcopy(obj->data+(PNTLONGS*i)+OFFSET_POINT,
  103.           obj->data+(PNTLONGS*i)+OFFSET_UVS,3*sizeof(long),1,inf);
  104.     }
  105.     fclose(inf);
  106.     return obj;
  107.     } 
  108.     if(magic == SOMAGIC) {
  109.     head = 0;
  110.     while(1) {
  111.         fread(&objtype,sizeof(long),1,inf);
  112.         if(objtype == OBJ_END)
  113.         break;
  114.         fread(&nlongs,sizeof(long),1,inf);
  115.         switch(objtype) {
  116.         case OBJ_QUADLIST:
  117.             nquads = (nlongs/PNTLONGS)/4;
  118.             obj  = newquadobj(nquads);
  119.             fread(obj->data,nlongs*sizeof(long),1,inf);
  120.             break;
  121.         case OBJ_TRILIST:
  122.             ntri = (nlongs/PNTLONGS)/3;
  123.             obj  = newtriobj(ntri);
  124.             fread(obj->data,nlongs*sizeof(long),1,inf);
  125.             break;
  126.         case OBJ_TRIMESH:
  127.             obj = newtmeshobj(nlongs);
  128.             fread(obj->data,nlongs*sizeof(long),1,inf);
  129.             break;
  130.         default:
  131.             fprintf(stderr,"readsgiobj: bad obj type %d\n",objtype);
  132.             exit(1);
  133.             break;
  134.         }
  135.         if(head == 0) {
  136.         head = tail = obj;
  137.         } else {
  138.         tail->next = obj;
  139.         tail = obj;
  140.         }
  141.     }
  142.     fclose(inf);
  143.     return head;
  144.     }
  145.     fprintf(stderr,"readsgiobj: bad magic %d in object file\n",magic);
  146.     exit(1);
  147. }
  148.  
  149.  
  150. #define BYTEOFF(v)    (sizeof(long)*(v))
  151.  
  152. drawsgiobj(obj,how)
  153. sgiobj *obj;
  154. int how;
  155. {
  156.     long npolys;
  157.     long *data;
  158.     char *vertdata, *avert;
  159.     int vertlongs, nverts;
  160.  
  161.     data = obj->data;
  162.     if(obj->objtype == OBJ_QUADLIST) {
  163.     npolys = (obj->nlongs/PNTLONGS)/4;
  164.     switch(how) {
  165.         case DRAW_POINTS:
  166.         while(npolys--) {
  167.             bgntmesh();
  168.             v3f((float*)&data[(PNTLONGS*0)+OFFSET_POINT]);
  169.             v3f((float*)&data[(PNTLONGS*1)+OFFSET_POINT]);
  170.             v3f((float*)&data[(PNTLONGS*3)+OFFSET_POINT]);
  171.             v3f((float*)&data[(PNTLONGS*2)+OFFSET_POINT]);
  172.             endtmesh();
  173.             data += PNTLONGS*4;
  174.         }
  175.         break;
  176.         case DRAW_POINTS|DRAW_NORMALS:
  177.         while(npolys--) {
  178.             bgntmesh();
  179.             n3f((float*)&data[(PNTLONGS*0)+OFFSET_NORMAL]);
  180.             v3f((float*)&data[(PNTLONGS*0)+OFFSET_POINT]);
  181.             n3f((float*)&data[(PNTLONGS*1)+OFFSET_NORMAL]);
  182.             v3f((float*)&data[(PNTLONGS*1)+OFFSET_POINT]);
  183.             n3f((float*)&data[(PNTLONGS*3)+OFFSET_NORMAL]);
  184.             v3f((float*)&data[(PNTLONGS*3)+OFFSET_POINT]);
  185.             n3f((float*)&data[(PNTLONGS*2)+OFFSET_NORMAL]);
  186.             v3f((float*)&data[(PNTLONGS*2)+OFFSET_POINT]);
  187.             endtmesh();
  188.             data += PNTLONGS*4;
  189.         }
  190.         break;
  191.         case DRAW_POINTS|DRAW_UVS:
  192.         while(npolys--) {
  193.             bgntmesh();
  194.             t2f((float*)&data[(PNTLONGS*0)+OFFSET_UVS]);
  195.             v3f((float*)&data[(PNTLONGS*0)+OFFSET_POINT]);
  196.             t2f((float*)&data[(PNTLONGS*1)+OFFSET_UVS]);
  197.             v3f((float*)&data[(PNTLONGS*1)+OFFSET_POINT]);
  198.             t2f((float*)&data[(PNTLONGS*3)+OFFSET_UVS]);
  199.             v3f((float*)&data[(PNTLONGS*3)+OFFSET_POINT]);
  200.             t2f((float*)&data[(PNTLONGS*2)+OFFSET_UVS]);
  201.             v3f((float*)&data[(PNTLONGS*2)+OFFSET_POINT]);
  202.             endtmesh();
  203.             data += PNTLONGS*4;
  204.         }
  205.         break;
  206.         case DRAW_POINTS|DRAW_UVS|DRAW_NORMALS:
  207.         while(npolys--) {
  208.             bgntmesh();
  209.             t2f((float*)&data[(PNTLONGS*0)+OFFSET_UVS]);
  210.             n3f((float*)&data[(PNTLONGS*0)+OFFSET_NORMAL]);
  211.             v3f((float*)&data[(PNTLONGS*0)+OFFSET_POINT]);
  212.             t2f((float*)&data[(PNTLONGS*1)+OFFSET_UVS]);
  213.             n3f((float*)&data[(PNTLONGS*1)+OFFSET_NORMAL]);
  214.             v3f((float*)&data[(PNTLONGS*1)+OFFSET_POINT]);
  215.             t2f((float*)&data[(PNTLONGS*3)+OFFSET_UVS]);
  216.             n3f((float*)&data[(PNTLONGS*3)+OFFSET_NORMAL]);
  217.             v3f((float*)&data[(PNTLONGS*3)+OFFSET_POINT]);
  218.             t2f((float*)&data[(PNTLONGS*2)+OFFSET_UVS]);
  219.             n3f((float*)&data[(PNTLONGS*2)+OFFSET_NORMAL]);
  220.             v3f((float*)&data[(PNTLONGS*2)+OFFSET_POINT]);
  221.             endtmesh();
  222.             data += PNTLONGS*4;
  223.         }
  224.         break;
  225.         case DRAW_POINTS|DRAW_COLORS:
  226.         while(npolys--) {
  227.             bgntmesh();
  228.             c3f((float*)&data[(PNTLONGS*0)+OFFSET_COLOR]);
  229.             v3f((float*)&data[(PNTLONGS*0)+OFFSET_POINT]);
  230.             c3f((float*)&data[(PNTLONGS*1)+OFFSET_COLOR]);
  231.             v3f((float*)&data[(PNTLONGS*1)+OFFSET_POINT]);
  232.             c3f((float*)&data[(PNTLONGS*3)+OFFSET_COLOR]);
  233.             v3f((float*)&data[(PNTLONGS*3)+OFFSET_POINT]);
  234.             c3f((float*)&data[(PNTLONGS*2)+OFFSET_COLOR]);
  235.             v3f((float*)&data[(PNTLONGS*2)+OFFSET_POINT]);
  236.             endtmesh();
  237.             data += PNTLONGS*4;
  238.         }
  239.         break;
  240.         case DRAW_POINTS|DRAW_COLORS|DRAW_NORMALS:
  241.         while(npolys--) {
  242.             bgntmesh();
  243.             c3f((float*)&data[(PNTLONGS*0)+OFFSET_COLOR]);
  244.             n3f((float*)&data[(PNTLONGS*0)+OFFSET_NORMAL]);
  245.             v3f((float*)&data[(PNTLONGS*0)+OFFSET_POINT]);
  246.             c3f((float*)&data[(PNTLONGS*1)+OFFSET_COLOR]);
  247.             n3f((float*)&data[(PNTLONGS*1)+OFFSET_NORMAL]);
  248.             v3f((float*)&data[(PNTLONGS*1)+OFFSET_POINT]);
  249.             c3f((float*)&data[(PNTLONGS*3)+OFFSET_COLOR]);
  250.             n3f((float*)&data[(PNTLONGS*3)+OFFSET_NORMAL]);
  251.             v3f((float*)&data[(PNTLONGS*3)+OFFSET_POINT]);
  252.             c3f((float*)&data[(PNTLONGS*2)+OFFSET_COLOR]);
  253.             n3f((float*)&data[(PNTLONGS*2)+OFFSET_NORMAL]);
  254.             v3f((float*)&data[(PNTLONGS*2)+OFFSET_POINT]);
  255.             endtmesh();
  256.             data += PNTLONGS*4;
  257.         }
  258.         break;
  259.     }
  260.     } else if(obj->objtype == OBJ_TRILIST) {
  261.     npolys = (obj->nlongs/PNTLONGS)/3;
  262.     switch(how) {
  263.         case DRAW_POINTS:
  264.         while(npolys--) {
  265.             bgntmesh();
  266.             v3f((float*)&data[(PNTLONGS*0)+OFFSET_POINT]);
  267.             v3f((float*)&data[(PNTLONGS*1)+OFFSET_POINT]);
  268.             v3f((float*)&data[(PNTLONGS*2)+OFFSET_POINT]);
  269.             endtmesh();
  270.             data += PNTLONGS*3;
  271.         }
  272.         break;
  273.         case DRAW_POINTS|DRAW_NORMALS:
  274.         while(npolys--) {
  275.             bgntmesh();
  276.             n3f((float*)&data[(PNTLONGS*0)+OFFSET_NORMAL]);
  277.             v3f((float*)&data[(PNTLONGS*0)+OFFSET_POINT]);
  278.             n3f((float*)&data[(PNTLONGS*1)+OFFSET_NORMAL]);
  279.             v3f((float*)&data[(PNTLONGS*1)+OFFSET_POINT]);
  280.             n3f((float*)&data[(PNTLONGS*2)+OFFSET_NORMAL]);
  281.             v3f((float*)&data[(PNTLONGS*2)+OFFSET_POINT]);
  282.             endtmesh();
  283.             data += PNTLONGS*3;
  284.         }
  285.         break;
  286.         case DRAW_POINTS|DRAW_UVS:
  287.         while(npolys--) {
  288.             bgntmesh();
  289.             t2f((float*)&data[(PNTLONGS*0)+OFFSET_UVS]);
  290.             v3f((float*)&data[(PNTLONGS*0)+OFFSET_POINT]);
  291.             t2f((float*)(float*)&data[(PNTLONGS*1)+OFFSET_UVS]);
  292.             v3f((float*)&data[(PNTLONGS*1)+OFFSET_POINT]);
  293.             t2f((float*)&data[(PNTLONGS*2)+OFFSET_UVS]);
  294.             v3f((float*)&data[(PNTLONGS*2)+OFFSET_POINT]);
  295.             endtmesh();
  296.             data += PNTLONGS*3;
  297.         }
  298.         break;
  299.         case DRAW_POINTS|DRAW_UVS|DRAW_NORMALS:
  300.         while(npolys--) {
  301.             bgntmesh();
  302.             n3f((float*)&data[(PNTLONGS*0)+OFFSET_NORMAL]);
  303.             t2f((float*)&data[(PNTLONGS*0)+OFFSET_UVS]);
  304.             v3f((float*)&data[(PNTLONGS*0)+OFFSET_POINT]);
  305.             n3f((float*)&data[(PNTLONGS*1)+OFFSET_NORMAL]);
  306.             t2f((float*)&data[(PNTLONGS*1)+OFFSET_UVS]);
  307.             v3f((float*)&data[(PNTLONGS*1)+OFFSET_POINT]);
  308.             n3f((float*)&data[(PNTLONGS*2)+OFFSET_NORMAL]);
  309.             t2f((float*)&data[(PNTLONGS*2)+OFFSET_UVS]);
  310.             v3f((float*)&data[(PNTLONGS*2)+OFFSET_POINT]);
  311.             endtmesh();
  312.             data += PNTLONGS*3;
  313.         }
  314.         break;
  315.         case DRAW_POINTS|DRAW_COLORS:
  316.         while(npolys--) {
  317.             bgntmesh();
  318.             c3f((float*)&data[(PNTLONGS*0)+OFFSET_COLOR]);
  319.             v3f((float*)&data[(PNTLONGS*0)+OFFSET_POINT]);
  320.             c3f((float*)&data[(PNTLONGS*1)+OFFSET_COLOR]);
  321.             v3f((float*)&data[(PNTLONGS*1)+OFFSET_POINT]);
  322.             c3f((float*)&data[(PNTLONGS*2)+OFFSET_COLOR]);
  323.             v3f((float*)&data[(PNTLONGS*2)+OFFSET_POINT]);
  324.             endtmesh();
  325.             data += PNTLONGS*3;
  326.         }
  327.         break;
  328.         case DRAW_POINTS|DRAW_COLORS|DRAW_NORMALS:
  329.         while(npolys--) {
  330.             bgntmesh();
  331.             c3f((float*)&data[(PNTLONGS*0)+OFFSET_COLOR]);
  332.             n3f((float*)&data[(PNTLONGS*0)+OFFSET_NORMAL]);
  333.             v3f((float*)&data[(PNTLONGS*0)+OFFSET_POINT]);
  334.             c3f((float*)&data[(PNTLONGS*1)+OFFSET_COLOR]);
  335.             n3f((float*)&data[(PNTLONGS*1)+OFFSET_NORMAL]);
  336.             v3f((float*)&data[(PNTLONGS*1)+OFFSET_POINT]);
  337.             c3f((float*)&data[(PNTLONGS*2)+OFFSET_COLOR]);
  338.             n3f((float*)&data[(PNTLONGS*2)+OFFSET_NORMAL]);
  339.             v3f((float*)&data[(PNTLONGS*2)+OFFSET_POINT]);
  340.             endtmesh();
  341.             data += PNTLONGS*3;
  342.         }
  343.         break;
  344.     }
  345.     } else if(obj->objtype == OBJ_TRIMESH) {
  346.     if(how == DRAW_LINES)
  347.         return;
  348.     else {
  349.         vertlongs = *data++;
  350.         vertdata = (char *)data;
  351.         data += vertlongs;
  352.         while(1) {
  353.         switch(*data++) {
  354.             case OP_BGNTMESH:
  355.             bgntmesh();
  356.             break;
  357.             case OP_SWAPTMESH:
  358.             swaptmesh();
  359.             break;
  360.             case OP_ENDBGNTMESH:
  361.             endtmesh();
  362.             bgntmesh();
  363.             break;
  364.             case OP_ENDTMESH:
  365.             endtmesh();
  366.             return;
  367.             default:
  368.             fprintf(stderr,"drawsgiobj: bad tmesh op %d\n",*data);
  369.             exit(1);
  370.         }
  371.         nverts = *data++;
  372.         switch(how) {
  373.             case DRAW_POINTS:
  374.             while(nverts--) {
  375.                 avert = vertdata + *data++;
  376.                 v3f((float*)(avert+BYTEOFF(OFFSET_POINT)));
  377.             }
  378.             break;
  379.             case DRAW_POINTS|DRAW_NORMALS:
  380.             while(nverts--) {
  381.                 avert = vertdata + *data++;
  382.                 n3f((float*)(avert+BYTEOFF(OFFSET_NORMAL)));
  383.                 v3f((float*)(avert+BYTEOFF(OFFSET_POINT)));
  384.             }
  385.             break;
  386.             case DRAW_POINTS|DRAW_UVS:
  387.             while(nverts--) {
  388.                 avert = vertdata + *data++;
  389.                 t2f((float*)(avert+BYTEOFF(OFFSET_UVS)));
  390.                 v3f((float*)(avert+BYTEOFF(OFFSET_POINT)));
  391.             }
  392.             break;
  393.             case DRAW_POINTS|DRAW_UVS|DRAW_NORMALS:
  394.             while(nverts--) {
  395.                 avert = vertdata + *data++;
  396.                 t2f((float*)(avert+BYTEOFF(OFFSET_UVS)));
  397.                 n3f((float*)(avert+BYTEOFF(OFFSET_NORMAL)));
  398.                 v3f((float*)(avert+BYTEOFF(OFFSET_POINT)));
  399.             }
  400.             break;
  401.             case DRAW_POINTS|DRAW_COLORS:
  402.             while(nverts--) {
  403.                 avert = vertdata + *data++;
  404.                 c3f((float*)(avert+BYTEOFF(OFFSET_COLOR)));
  405.                 v3f((float*)(avert+BYTEOFF(OFFSET_POINT)));
  406.             }
  407.             break;
  408.             case DRAW_POINTS|DRAW_COLORS|DRAW_NORMALS:
  409.             while(nverts--) {
  410.                 avert = vertdata + *data++;
  411.                 c3f((float*)(avert+BYTEOFF(OFFSET_COLOR)));
  412.                 n3f((float*)(avert+BYTEOFF(OFFSET_NORMAL)));
  413.                 v3f((float*)(avert+BYTEOFF(OFFSET_POINT)));
  414.             }
  415.         }
  416.         }
  417.     }
  418.     } else {
  419.     fprintf(stderr,"drawsgiobj: bad object type %d\n",obj->objtype);
  420.     exit(1);
  421.     }
  422. }
  423.